package com.baidu.baidumap;

import android.os.Bundle;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.baidu.mapapi.overlayutil.PoiOverlay;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiBoundSearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;

/**
 * 附近搜索
 */
public class PoiSearchNearByDemo extends BaseActivity {
    private PoiSearch poiSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        search();
    }

    private void search() {


        poiSearch = PoiSearch.newInstance();
        poiSearch.setOnGetPoiSearchResultListener(new MyListener());

        PoiNearbySearchOption nearbyOption = new PoiNearbySearchOption();
        nearbyOption.location(hmPos);// 设置中心点
        nearbyOption.radius(1000);// 设置半径 单位是米
        nearbyOption.keyword("加油站");// 关键字
        poiSearch.searchNearby(nearbyOption);
    }

    class MyListener implements OnGetPoiSearchResultListener{



        @Override
        public void onGetPoiResult(PoiResult poiResult) {
            if (poiResult==null|| SearchResult.ERRORNO.RESULT_NOT_FOUND==poiResult.error){
                Toast.makeText(getApplicationContext(),"未搜索到结果",Toast.LENGTH_SHORT).show();
                return;
            }
            PoiOverlay overlay = new MyPoiOverLay(baiduMap);//搜索poi的覆盖物
            baiduMap.setOnMarkerClickListener(overlay);
            overlay.setData(poiResult);
            overlay.addToMap();
            overlay.zoomToSpan();
        }

        @Override
        public void onGetPoiDetailResult(PoiDetailResult result) {
            if (result==null|| SearchResult.ERRORNO.RESULT_NOT_FOUND==result.error){
                Toast.makeText(getApplicationContext(),"未搜索到结果",Toast.LENGTH_SHORT).show();
                return;
            }
            String  text = result.getAddress()+"::"+result.getCommentNum()+result.getEnvironmentRating();
            Toast.makeText(getApplicationContext(),text,Toast.LENGTH_SHORT).show();

        }


    }

    class MyPoiOverLay extends PoiOverlay{

        public MyPoiOverLay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public boolean onPoiClick(int i) {
            PoiResult poiResult = getPoiResult();
            PoiInfo poiInfo = poiResult.getAllPoi().get(i);
            String text = poiInfo.name+","+poiInfo.address;
           Toast.makeText(getApplicationContext(),text,Toast.LENGTH_SHORT).show();

            PoiDetailSearchOption detailOption = new PoiDetailSearchOption();
            detailOption.poiUid(poiInfo.uid);
            poiSearch.searchPoiDetail(detailOption);
            return super.onPoiClick(i);
        }
    }
}
